package cn.tedu.study.videomodel.controller;


import cn.tedu.study.response.ResultVO;
import cn.tedu.study.response.StatusCode;
import cn.tedu.study.security.CustomUserDetail;
import cn.tedu.study.videomodel.mapper.VideoMapper;
import cn.tedu.study.videomodel.pojo.dto.VideoInsertDTO;
import cn.tedu.study.videomodel.pojo.entity.Video;
import cn.tedu.study.videomodel.pojo.vo.ListContentVO;
import cn.tedu.study.videomodel.pojo.vo.ListVideoDetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/video/")
public class VideoController {
    @Value("${filePath}")
    private String filePath;

    @Autowired
    private VideoMapper videoMapper;

    @RequestMapping("insert")
    public ResultVO postVideo(@RequestBody VideoInsertDTO videoInsertDTO,
                              @AuthenticationPrincipal CustomUserDetail customUserDetail) {
        System.out.println("videoInsertDTO = " + videoInsertDTO);
        Video video = new Video();
        BeanUtils.copyProperties(videoInsertDTO, video);

        video.setCreateTime(new Date());
        video.setLevel(1);
        video.setUserId(customUserDetail.getId());
        videoMapper.insert(video);
        return ResultVO.ok();

    }

    @PostMapping("/{id}/deleteById")
    public ResultVO deleteById(@PathVariable Integer id){
        videoMapper.deleteById(id);
        return ResultVO.ok();
    }

    @RequestMapping("/{id}/updateById")
    public ResultVO updateById(@RequestBody VideoInsertDTO videoInsertDTO){
        Video video = new Video();
        BeanUtils.copyProperties(videoInsertDTO, video);
        video.setUpdateTime(new Date());
        videoMapper.updateById(video);
        return ResultVO.ok();
    }

    @RequestMapping("listContent")
    public ResultVO listContent(@PathVariable Integer id,@AuthenticationPrincipal CustomUserDetail customUserDetail){
        if (customUserDetail == null){
            return new ResultVO(StatusCode.NOT_LOGIN);
        }
        List<ListContentVO> list = videoMapper.selectListContent(id);
        return ResultVO.ok(list);
    }

    @RequestMapping("listVideoDetail")
    public ResultVO ListVideoDetailByUserId(@AuthenticationPrincipal CustomUserDetail customUserDetail) {
        if (customUserDetail == null){
            return new ResultVO(StatusCode.NOT_LOGIN);
        }
        List<ListVideoDetailVO> listVideoDetailVO = videoMapper.ListVideoDetailByUserId(customUserDetail.getId());

        return ResultVO.ok(listVideoDetailVO);
    }
}
